<template>
  <div class="app-container">
    <div>
      <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
        <el-menu-item index="0">
          图标导航配置
        </el-menu-item>
        <el-menu-item index="1">
          各模块头图配置
        </el-menu-item>
        <el-menu-item index="2">
          底部导航栏配置
        </el-menu-item>
      </el-menu>
    </div>
    <el-form v-if="activeIndex ==='0'" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="小程序首页标题：" style="margin-top: 30px" label-width="180px">
        <el-input v-model="list[0].value" placeholder="请输入内容"/>
      </el-form-item>
      <div style="display: flex;justify-content: space-evenly" >
        <div v-for="(item,index) in list[6].value" style="width: 300px;height: 400px;background-color: #FFFFFF">
          <!--          上半部分-->
          <div style="display: flex;justify-content: center;align-items: center;width: 100%;height: 140px;border-width: 0px 0px 1px 0px;border-color: #DFE4ED;border-style: solid;">
            <div style="width: 100px;width: 100px;align-text: center">
              <div style="width: 100%;display: flex;justify-content: center">
                <el-image
                  style="width: 50px; height: 50px"
                  :src="item.icon"
                  fit="fit"
                ></el-image>
              </div>
              <div style="margin-top: 20px; width: 100%;display: flex;justify-content: center">{{item.name}}</div>
            </div>
          </div>
          <!--          下半部分-->
          <div>
            <el-form-item label="名称：" style="margin-top: 30px">
              <el-input style="width: 150px" v-model="item.name" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="修改图标：" style="margin-top: 30px">
              <el-upload
                class="avatar-uploader"
                action
                :http-request="createUploader(index)"
                :show-file-list="false"
              >
                <img v-if="item.icon" :src="item.icon" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="该模块：">
              <el-switch
                v-model="item.status"
                active-text="显示"
                inactive-text="不显示">
              </el-switch>
            </el-form-item>
          </div>
        </div>
      </div>
      <div style="display: flex;justify-content: center;margin-top: 50px">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-form>
    <el-form v-if="activeIndex ==='1'" ref="form" :model="form" :rules="rules" label-width="80px">
      <div style="display: flex;justify-content: space-evenly" >
        <div v-for="(item,index) in list" v-if="item.category == 3" style="width: 300px;height: 400px;background-color: #FFFFFF;margin-top: 40px">
          <!--          上半部分-->
          <div  style="display: flex;justify-content: center;align-items: center;width: 100%;height: 140px;border-width: 0px 0px 1px 0px;border-color: #DFE4ED;border-style: solid;">
            <div style="width: 100%;align-text: center;height: 140px">
              <div style="width: 100%;display: flex;justify-content: center">
                <el-image
                  style="width: 100%;height: 140px"
                  :src="item.value"
                  fit="fit"
                ></el-image>
              </div>
            </div>
          </div>
          <div style="margin-top: 20px; width: 100%;display: flex;justify-content: center">{{item.title}}</div>
          <!--          下半部分-->
          <div>
            <el-form-item label="修改图标：" style="margin-top: 30px">
              <el-upload
                class="avatar-uploader"
                action
                :http-request="createUploader1(index)"
                :show-file-list="false"
              >
                <img v-if="item.value" :src="item.value" class="avatar1">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </div>
        </div>

      </div>
      <div style="display: flex;justify-content: center;margin-top: 50px">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-form>
    <el-form v-if="activeIndex ==='2'" ref="form" :model="form" :rules="rules" label-width="100px">
      <div style="display: flex;justify-content: space-evenly;margin-top: 20px" >
        <div v-for="(item,index) in list[5].value" style="width: 280px;height: 350px;background-color: #FFFFFF">
          <!--          上半部分-->
          <div style="display: flex;justify-content: center;align-items: center;width: 100%;height: 140px;border-width: 0px 0px 1px 0px;border-color: #DFE4ED;border-style: solid;">
            <div style="width: 100px;width: 100px;align-text: center">
              <div style="width: 100%;display: flex;justify-content: center">
                <el-image
                  style="width: 50px; height: 50px"
                  :src="item.iconPath"
                  fit="fit"
                ></el-image>
              </div>
              <div style="margin-top: 20px; width: 100%;display: flex;justify-content: center">{{item.text}}</div>
            </div>
          </div>
          <!--          下半部分-->
          <div>
            <el-form-item label="名称：" style="margin-top: 30px">
              <el-input style="width: 150px" v-model="item.text" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="修改图标：" style="margin-top: 10px">
              <el-upload
                class="avatar-uploader"
                action
                :http-request="createUploader(index)"
                :show-file-list="false"
              >
                <img v-if="item.iconPath" :src="item.iconPath" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="该模块：">
              <el-switch
                v-model="item.status"
                active-text="显示"
                inactive-text="不显示">
              </el-switch>
            </el-form-item>
          </div>
        </div>
      </div>
      <div style="display: flex;justify-content: space-evenly;margin-top: 20px" >
        <div v-for="(item,index) in list[5].value" style="width: 280px;height: 350px;background-color: #FFFFFF">
          <!--          上半部分-->
          <div style="display: flex;justify-content: center;align-items: center;width: 100%;height: 140px;border-width: 0px 0px 1px 0px;border-color: #DFE4ED;border-style: solid;">
            <div style="width: 100px;width: 100px;align-text: center">
              <div style="width: 100%;display: flex;justify-content: center">
                <el-image
                  style="width: 50px; height: 50px"
                  :src="item.selectedIconPath"
                  fit="fit"
                ></el-image>
              </div>
              <div style="margin-top: 20px; width: 100%;display: flex;justify-content: center">{{item.text}}</div>
            </div>
          </div>
          <!--          下半部分-->
          <div>
            <el-form-item label="名称：" style="margin-top: 30px">
              <el-input style="width: 150px" v-model="item.text" placeholder="请输入内容"/>
            </el-form-item>
            <el-form-item label="修改图标：" style="margin-top: 10px">
              <el-upload
                class="avatar-uploader"
                action
                :http-request="createUploader(index)"
                :show-file-list="false"
              >
                <img v-if="item.selectedIconPath" :src="item.selectedIconPath" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="该模块：">
              <el-switch
                v-model="item.status"
                active-text="显示"
                inactive-text="不显示">
              </el-switch>
            </el-form-item>
          </div>
        </div>
      </div>
      <div style="display: flex;justify-content: center;margin-top: 50px">
        <el-button type="primary" @click="submitForm">保存</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { applet, appletupdate } from '@/api/role'
import { uploadPic } from '@/api/activity/activity'

export default {
  name: 'index',
  data(){
    return {
      activeIndex:'0',
      form:{},
      list:[],
      status_switch:false,
      rules: {
        remark: [
          { required: true, message: '请输入信息', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.list = []
    applet().then(res =>{
      this.list = res
      this.list[6].value = JSON.parse(res[6].value)
      this.list[5].value = JSON.parse(res[5].value)
      console.error(JSON.parse(res[6].value))
    })
  },
  methods:{
    createUploader(index) {
      return (options) => {
        this.selectPicUpload(index, options);
      };
    },
    selectPicUpload(e,obj){
      console.log(obj,e,'1111111111111')
      let fd = new FormData() //参数的格式是formData格式的
      fd.append('file', obj.file) //参数
      console.log(fd)
      uploadPic(fd, { modularName: 'SUPERMARKET' }).then(res => {
        this.list[6].value[e].icon = res
      })
    },
    // 首图修改
    createUploader1(index) {
      return (options) => {
        this.selectPicUpload1(index, options);
      };
    },
    selectPicUpload1(e,obj){
      console.log(obj,e,'1111111111111')
      let fd = new FormData() //参数的格式是formData格式的
      fd.append('file', obj.file) //参数
      console.log(fd)
      uploadPic(fd, { modularName: 'SUPERMARKET' }).then(res => {
        this.list[e].value = res
      })
    },
    handleSelect(key, keyPath) {
      this.activeIndex = key
      console.log(key, keyPath)
    },
    submitForm(){
      this.list[6].value = JSON.stringify(this.list[6].value)
      this.list[5].value = JSON.stringify(this.list[5].value)
      appletupdate(this.list).then(res =>{
        this.list[6].value = JSON.parse(this.list[6].value)
        this.list[5].value = JSON.parse(this.list[5].value)
      })
    }
  }
}
</script>

<style scoped  lang="scss">
.avatar {
  width: 40px;
  height: 40px;
  display: block;
}
.avatar1{
  width: 120px;
  height: 90px;
}
</style>
